﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;

namespace TowerDef_s_enseEditor
{
    public partial class Editor : Form
    {
        #region variables

        readonly Map _map = new Map();

        List<MapButton> _buttonList = new List<MapButton>();

        int[,] _waypoints;
        int _index;
        int[] _mapPoints;

        #endregion

        #region functions

        /// <summary>
        /// Editor constructor
        /// </summary>
        public Editor()
        {
            InitializeComponent();

            _map.InitializeMap(this);
            EnableDisableStartButton();
            EnableDisableGoalButton();
        }

        /// <summary>
        /// Set the standard flatstyles of the Editorbuttons to Popup
        /// </summary>
        private void SetStandardFlatStyle()
        {
            buttonWiese.FlatStyle = FlatStyle.Popup;
            buttonStart.FlatStyle = FlatStyle.Popup;
            buttonWeg.FlatStyle = FlatStyle.Popup;
            buttonGoal.FlatStyle = FlatStyle.Popup;
            buttonLöschen.FlatStyle = FlatStyle.Popup;
        }

        /// <summary>
        /// Enables and diables the buttons 
        /// </summary>
        /// <param name="imageNumber">Number and art of the images</param>
        public void EnableDisableButtons(int imageNumber)
        {
            switch (imageNumber)
            {
                case 0:
                    buttonWeg.Enabled = true;
                    buttonWeg.BackgroundImage = Properties.Resources.path_1;
                    buttonGoal.Enabled = true;
                    buttonGoal.BackgroundImage = Properties.Resources.goal_1;
                    buttonStart.Enabled = false;
                    buttonStart.BackgroundImage = Properties.Resources.start_1_disabled;

                    EnableDisableGoalButton();

                    _map.CurrentTexture = "2";
                    SetStandardFlatStyle();
                    buttonWeg.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
                    break;
                case 1:
                    EnableDisableStartButton();
                    EnableDisableGoalButton();
                    break;
                case 2:
                    EnableDisableStartButton();
                    EnableDisableGoalButton();
                    break;
                case 3:
                    buttonGoal.Enabled = false;
                    buttonGoal.BackgroundImage = Properties.Resources.goal_1_disabled;

                    EnableDisableStartButton();

                    _map.CurrentTexture = "2";
                    SetStandardFlatStyle();
                    buttonWeg.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
                    break;
            }
        }

        /// <summary>
        /// Enable or disable the startbutton depending on the occurence in the map
        /// </summary>
        private void EnableDisableStartButton()
        {
            if (!_map.StartIsset())
            {
                buttonStart.Enabled = true;
                buttonStart.BackgroundImage = Properties.Resources.start_1;
            }
            else
            {
                buttonStart.Enabled = false;
                buttonStart.BackgroundImage = Properties.Resources.start_1_disabled;
            }
        }

        /// <summary>
        /// Enable or disable the goalbutton depending on the occurence in the map
        /// </summary>
        private void EnableDisableGoalButton()
        {
            if (!_map.GoalIsset())
            {
                buttonGoal.Enabled = true;
                buttonGoal.BackgroundImage = Properties.Resources.goal_1;
            }
            else
            {
                buttonGoal.Enabled = false;
                buttonGoal.BackgroundImage = Properties.Resources.goal_1_disabled;
            }
        }

        /// <summary>
        /// Load the map from a map file and read the texture indexes
        /// </summary>
        private void ReadMpFile()
        {
            var streamReader = new StreamReader(openFileDialogImportieren.FileName);

            _index = 0;
            string text = streamReader.ReadToEnd();
            var c = new char[text.Length];
            _mapPoints = new int[Convert.ToInt32(Convert.ToDouble(text.Length - 18) / 2)];

            var stringReader = new StringReader(text);
            stringReader.Read(c, 0, text.Length);

            for (int y = 0; y < text.Length; y++)
            {
                if (c[y] != 10 && c[y] != 44)
                {
                    _mapPoints[_index] = Convert.ToInt16(Convert.ToString(c[y]));
                    _index += 1;
                }
            }
        }

        /// <summary>
        /// Export the designed map and save them to a .mp file
        /// </summary>
        private void SaveMpFile()
        {
            StreamWriter streamWriter = File.CreateText(saveFileDialogSpeichernUnter.FileName);

            _index = 0;
            this._buttonList = _map.ButtonList;

            for (int y = 0; y < 18; y++)
            {
                for (int x = 0; x < 18; x++)
                {
                    streamWriter.Write(_buttonList.ElementAt<MapButton>(_index).ImageNumber + ",");
                    if (x == 17)
                    {
                        streamWriter.Write("\n");
                    }
                    _index++;
                }
            }

            streamWriter.Close();
        }

        #endregion

        #region events

        private void ButtonWieseClick(object sender, EventArgs e)
        {
            _map.CurrentTexture = "1";
            SetStandardFlatStyle();
            buttonWiese.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
        }

        private void ButtonStartClick(object sender, EventArgs e)
        {
            _map.CurrentTexture = "0";
            SetStandardFlatStyle();
            buttonStart.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
        }

        private void ButtonWegClick(object sender, EventArgs e)
        {
            _map.CurrentTexture = "2";
            SetStandardFlatStyle();
            buttonWeg.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
        }

        private void ButtonGoalClick(object sender, EventArgs e)
        {
            _map.CurrentTexture = "3";
            SetStandardFlatStyle();
            buttonGoal.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
        }

        private void ButtonLoeschenClick(object sender, EventArgs e)
        {
            _map.CurrentTexture = "1";
            SetStandardFlatStyle();
            buttonLöschen.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
        }

        private void NeueMapErstellenToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (MessageBox.Show("Wollen Sie wirklich eine leere Map laden?\n" +
                "Nicht gespeicherte Änderungen gehen verloren! Vergessen Sie nicht zu speichern!",
                "Achtung!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                _map.ClearMap();
                EnableDisableStartButton();
                EnableDisableGoalButton();
            }
        }

        private void ZufallsmapErstellenToolStripMenuItemClick(object sender, EventArgs e)
        {

            //_map.GrassConflictCheck();
        }

        private void MapImportierenToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (MessageBox.Show("Wollen Sie wirklich eine neue Map laden?\n" +
                "Nicht gespeicherte Änderungen gehen verloren! Vergessen Sie nicht zu speichern!",
                "Achtung!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (openFileDialogImportieren.ShowDialog() == DialogResult.OK)
                {
                    ReadMpFile();
                    _map.SetButtonImages(_mapPoints, _index);
                    EnableDisableStartButton();
                    EnableDisableGoalButton();
                }
            }
        }

        private void MapExportierenToolStripMenuItemClick(object sender, EventArgs e)
        {
            saveFileDialogSpeichernUnter = new SaveFileDialog { Filter = "Mapfile|*.mp", Title = "Speichere deine Map" };
            saveFileDialogSpeichernUnter.ShowDialog();

            if (saveFileDialogSpeichernUnter.FileName != "")
            {
                SaveMpFile();
            }
        }

        private void EditorSchließenToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (MessageBox.Show("Wollen Sie den Editor wirklich schließen?\n" +
                "Nicht gespeicherte Änderungen gehen verloren! Vergessen Sie nicht zu speichern!",
                "Achtung!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void ToolStripMenuItemQuestionMarkClick(object sender, EventArgs e)
        {
            MessageBox.Show("TowerDef(s)ense Mapeditor\n" +
                "Version: 0.9 beta");
        }

        #endregion
    }
}
